package cn.zyq.ykb.system.config.weixin;


import cn.binarywang.wx.miniapp.api.WxMaService;
import cn.binarywang.wx.miniapp.api.impl.WxMaServiceImpl;
import cn.binarywang.wx.miniapp.config.WxMaConfig;
import cn.binarywang.wx.miniapp.config.impl.WxMaDefaultConfigImpl;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.api.impl.WxMpServiceImpl;
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
import me.chanjar.weixin.mp.config.impl.WxMpDefaultConfigImpl;
import me.chanjar.weixin.open.api.WxOpenComponentService;
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
import me.chanjar.weixin.open.api.WxOpenMpService;
import me.chanjar.weixin.open.api.WxOpenService;
import me.chanjar.weixin.open.api.impl.WxOpenComponentServiceImpl;
import me.chanjar.weixin.open.api.impl.WxOpenInMemoryConfigStorage;
import me.chanjar.weixin.open.api.impl.WxOpenMpServiceImpl;
import me.chanjar.weixin.open.api.impl.WxOpenServiceImpl;
import org.springframework.context.annotation.Configuration;

@Slf4j
@Configuration
public class WxConfig {


    /**
     * 小程序配置
     *
     * @return
     */
    public static WxMaConfig wxMaConfig() {
        WxMaDefaultConfigImpl config = new WxMaDefaultConfigImpl();
        config.setAppid(WxMaProperties.appId);
        config.setSecret(WxMaProperties.appSecret);
        return config;
    }

    /**
     * 小程序配置
     *
     * @return
     */
    public static WxMaService wxMaService() {
        WxMaService service = new WxMaServiceImpl();
        service.setWxMaConfig(wxMaConfig());
        return service;
    }

    /**
     * 微信开放平台app应用配置
     *
     * @return
     */
    public static WxOpenConfigStorage wxOpenConfigStorage() {
        WxOpenConfigStorage storage = new WxOpenInMemoryConfigStorage();
        storage.setComponentAppId(WxAppProperties.appId);
        storage.setComponentAppSecret(WxAppProperties.appSecret);
        return storage;
    }

    /**
     * 微信开放平台应用配置
     *
     * @return
     */
    public static WxOpenService wxOpenService() {
        WxOpenService service = new WxOpenServiceImpl();
        service.setWxOpenConfigStorage(wxOpenConfigStorage());
        return service;
    }

    /**
     * 微信开放平台app应用配置
     *
     * @return
     */
    public static WxMpConfigStorage wxMpConfigStorage() {
        WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
        config.setAppId(WxAppProperties.appId);
        config.setSecret(WxAppProperties.appSecret);
        return config;
    }

    /**
     * 微信开放平台app应用配置
     *
     * @return
     */
    public static WxOpenMpService wxOpenMpService() {
        log.info("appid:{}", WxAppProperties.appId);
        log.info("secret:{}", WxAppProperties.appSecret);
        WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
        config.setAppId(WxAppProperties.appId);
        config.setSecret(WxAppProperties.appSecret);
        WxOpenComponentService wxOpenComponentService = new WxOpenComponentServiceImpl(wxOpenService());
        return new WxOpenMpServiceImpl(wxOpenComponentService, WxAppProperties.appId, wxMpConfigStorage());
    }


    /**
     * 微信公众号应用配置
     *
     * @return
     */
    public static WxMpService wxMpService() {
        WxMpDefaultConfigImpl config = new WxMpDefaultConfigImpl();
        config.setAppId(WxAppProperties.appId);
        config.setSecret(WxAppProperties.appSecret);
        WxMpService service = new WxMpServiceImpl();
        service.setWxMpConfigStorage(config);
        return service;
    }


}
